package com.solution.admin.customer.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.solution.admin.customer.dao.CompanyExpressConfigAddressDao;
import com.solution.admin.customer.dao.CompanyExpressConfigDao;
import com.solution.admin.customer.entity.CompanyExpressConfigAddressEntity;
import com.solution.admin.customer.entity.CompanyExpressConfigEntity;
import com.solution.admin.customer.service.CompanyExpressConfigService;
import com.solution.common.dao.BaseDao;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;
import com.solution.common.utils.string.StringUtil;

/**
 * 公司常用物流配置service实现层
 * @author solin
 * @date 2018-03-22 18:49:13
 */
@Service
public class CompanyExpressConfigServiceImpl extends BaseServiceImpl<CompanyExpressConfigEntity> implements CompanyExpressConfigService {

	/**
	 * 公司常用物流配置接口
	 */
	@Autowired
	private CompanyExpressConfigDao companyExpressConfigDao;
	
	@Autowired
	private CompanyExpressConfigAddressDao companyExpressConfigAddressDao;

	protected BaseDao<CompanyExpressConfigEntity> getDao() {
		return this.companyExpressConfigDao;
	}

	/**
	 * 保存公司常用物流配置
	 * @param companyExpressConfigEntity 公司常用物流配置实体
	 * @return Long 添加成功公司常用物流配置的ID
	 * @author solin
	 * @date 2018-03-22 18:49:13
	 */
	public long saveCompanyExpressConfig(CompanyExpressConfigEntity companyExpressConfigEntity) {
		return companyExpressConfigDao.saveCompanyExpressConfig(companyExpressConfigEntity);
	}
	
	/**
	 * 保存常用物流
	 * @param companyExpressConfigList
	 * @param companyExpressConfigAddressList1 普通物流免运费地址
	 * @param companyExpressConfigAddressList2 快递物流免运费地址
	 * @return
	 * @author: solin
	 * @date: 2018年3月23日 下午3:19:38
	 */
	@Transactional(rollbackFor=Exception.class)
	public long saveCompanyExpressConfig(List<CompanyExpressConfigEntity> companyExpressConfigList, 
			List<CompanyExpressConfigAddressEntity> companyExpressConfigAddressList1,
			List<CompanyExpressConfigAddressEntity> companyExpressConfigAddressList2) {
		if (null != companyExpressConfigList && companyExpressConfigList.size() > 0) {
			companyExpressConfigDao.deleteCompanyExpressConfigByCompanyId(companyExpressConfigList.get(0).getCompanyId());
			companyExpressConfigAddressDao.deleteCompanyExpressConfigAddressByCompanyId(companyExpressConfigList.get(0).getCompanyId());
		}
		//long result = companyExpressConfigDao.saveCompanyExpressConfig(companyExpressConfigList);
		long result = 0;
		for (CompanyExpressConfigEntity companyExpressConfig : companyExpressConfigList) { 
			result = companyExpressConfigDao.saveCompanyExpressConfig(companyExpressConfig);
			if (result > 0) {
				if (companyExpressConfig.getExpressType() == 1) {
					for (int i = 0; i < companyExpressConfigAddressList1.size(); i++) {
						companyExpressConfigAddressList1.get(i).setExpressConfigId(result);
					}
					companyExpressConfigAddressDao.saveCompanyExpressConfigAddress(companyExpressConfigAddressList1);
				} else if (companyExpressConfig.getExpressType() == 3) {
					for (int i = 0; i < companyExpressConfigAddressList2.size(); i++) {
						companyExpressConfigAddressList2.get(i).setExpressConfigId(result);
					}
					companyExpressConfigAddressDao.saveCompanyExpressConfigAddress(companyExpressConfigAddressList2);
				}
			}
		}
		
		return result;
	}

	/**
	 * 分页查询公司常用物流配置
	 * @param pageParam 分页参数
	 * @param paramMap 查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author solin
	 * @date 2018-03-22 18:49:13
	 */
	@Override
	public PageBean listCompanyExpressConfigPage(PageParam pageParam, Map<String, Object> paramMap) {
		return companyExpressConfigDao.listCompanyExpressConfigPage(pageParam, paramMap);
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param id 用户id
	 * @return companyExpressConfigEntity 用户对象
	 * @author solin
	 * @date 2018-03-22 18:49:13
	 */
	public CompanyExpressConfigEntity getCompanyExpressConfigById(Long id) {
		return companyExpressConfigDao.getById(id.longValue());
	}

	/**
	 * 更新用户
	 * @param companyExpressConfigEntity 用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author solin
	 * @date 2018-03-22 18:49:13
	 */
	@Override
	public int updateCompanyExpressConfig(CompanyExpressConfigEntity companyExpressConfigEntity) {
		return companyExpressConfigDao.updateCompanyExpressConfig(companyExpressConfigEntity);
	}
	
	
	/**
	 * 根据条件查询公司常用物流配置
	 * @param param
	 * @return 数据列表
	 * @author solin
	 * @date 2018-03-22 18:49:13
	 */
	@Override
	public List<CompanyExpressConfigEntity> listCompanyExpressConfig( Map<String, Object> param){
		return  companyExpressConfigDao.listCompanyExpressConfig(param);
	}
	
	/**
	 * 删除公司常用物流配置
	 * @param 
	 * @return int 返回删除记录条数
	 * @author solin
	 * @date 2018-03-22 18:49:13
	 */
	public int deleteCompanyExpressConfigById(long id){
		return  companyExpressConfigDao.deleteById(id);
	}
	
	public int deleteCompanyExpressConfigByCompanyId(long companyId){
		return  companyExpressConfigDao.deleteCompanyExpressConfigByCompanyId(companyId);
	}
	
}
